package com.itzyf.pokemondata.activity.setting

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import cn.jpush.android.api.JPushInterface
import com.itzyf.pokemondata.BuildConfig
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.activity.gallery.WebViewActivity
import com.itzyf.pokemondata.bean.DbComprehensiveTypeDo
import com.itzyf.pokemondata.bean.DbExclusive
import com.itzyf.pokemondata.bean.DbPokemon
import com.itzyf.pokemondata.service.DataIntentService
import com.tencent.bugly.beta.Beta
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_about_app.*
import org.litepal.LitePal


/**
 * @author 依风听雨
 * @version 创建时间：2019/01/14 10:37
 */
class AboutAppActivity : BaseActivity() {
    private var receiver: LoadingBroadcast? = null
    override fun handlerActivity() {

        tvCheckUpdate.setOnClickListener {
            Beta.checkUpgrade()
        }

        tvVersion.text = getString(R.string.version, BuildConfig.VERSION_NAME)
        tvClear.setOnClickListener {
            sp.edit()
                .putBoolean(BuildConfig.DATA_VERSION, false)
                .apply()
            startService(Intent(this, DataIntentService::class.java))
        }

        receiver = LoadingBroadcast()
        val intentFilter = IntentFilter()
        intentFilter.addAction("loading")
        registerReceiver(receiver, intentFilter)


        cbHunting.isChecked = sp.getBoolean("isHuntingPush", true)
        cbHunting.setOnCheckedChangeListener { _, isChecked ->
            sp.edit().putBoolean("isHuntingPush", isChecked).apply()
            if (isChecked) {
                JPushInterface.addTags(this, 1, mutableSetOf("hunting"))
            } else {
                JPushInterface.deleteTags(this, 1, mutableSetOf("hunting"))
            }
        }

        cbActivity.isChecked = sp.getBoolean("isActivityPush", true)
        cbActivity.setOnCheckedChangeListener { _, isChecked ->
            sp.edit().putBoolean("isActivityPush", isChecked).apply()
            if (isChecked) {
                JPushInterface.addTags(this, 1, mutableSetOf("history"))
            } else {
                JPushInterface.deleteTags(this, 1, mutableSetOf("history"))
            }
        }

        tvReward.setOnClickListener {
            startActivity(Intent(this, RewardActivity::class.java))
        }
        tvReward2.setOnClickListener {
            val intent = Intent(this@AboutAppActivity, WebViewActivity::class.java)
            intent.putExtra("httpUrl", "https://ityuanfeng.gitee.io/#/reward")
            intent.putExtra("title", "打赏公示")
            startActivity(intent)
        }
        tvAbout.setOnClickListener {
            val intent = Intent(this@AboutAppActivity, AboutUsActivity::class.java)
//            intent.putExtra("httpUrl", "https://zouyuanfeng.github.io/about.html")
//            intent.putExtra("title", "关于APP")
            startActivity(intent)
        }

        tvFeedback.setOnClickListener {
            startActivity(Intent(this, FeedbackActivity::class.java))
        }
        tvFont.setOnClickListener {
            startActivity(Intent(this, FontActivity::class.java))
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (receiver != null) {
            unregisterReceiver(receiver)
        }
    }

    override fun getContentViewId() = R.layout.activity_about_app

    override fun getToolbarTitle() = "关于app"


    inner class LoadingBroadcast : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            intent?.let {
                if (it.getIntExtra("loading", -1) != -1) {
                    showProgressDialog()
                } else {
                    dismissProgressDialog()
                }
            }
        }

    }

}